//
//  TabBarViewController.swift
//  MZTool_Example
//
//  Created by gby on 2023/2/1.
//  Copyright © 2023 CocoaPods. All rights reserved.
//

import UIKit
import Foundation
import MZTool

class TabBarViewController: UITabBarController, UITabBarControllerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
        
        if #available(iOS 13.0, *) {
            self.overrideUserInterfaceStyle = .light
        }
        UITabBarItem.appearance().titlePositionAdjustment = UIOffset.init(horizontal: 0, vertical: -1)
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:MZ.RGB("#CBCBCB") as Any,
                                                          NSAttributedString.Key.font:UIFont.mz.size(11)],
                                                         for: UIControl.State.normal)
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:MZ.RGB("#81B6FF") as Any,
                                                          NSAttributedString.Key.font:UIFont.mz.size(11)],
                                                         for: UIControl.State.selected)
        
        UITabBar.appearance().shadowImage = UIImage.mz.color(color: MZ.RGB("#ffffff"))
        UITabBar.appearance().backgroundColor = MZ.RGB("#FFFFFF")
        UITabBar.appearance().backgroundImage = UIImage.mz.color(color: UIColor.white)
        
        self.tabBar.unselectedItemTintColor = MZ.RGB("#ffffff")
        self.tabBar.layer.shadowColor = MZ.RGB("#A1A1A1").cgColor
        self.tabBar.layer.shadowRadius = 10.0
        self.tabBar.layer.shadowOffset = CGSize(width: 0.0, height: -3.0)
        self.tabBar.layer.shadowOpacity = 0.16
        
        var ctrols:[UIViewController] = []
        
        func createData( ctrs: inout [UIViewController], vc: UIViewController, title: String?, image: UIImage?, selectedImage: UIImage?) {
            let navi = createTabbarChildViewController(vc, title: title)
            ctrs.append(navi)
            navi.tabBarItem = UITabBarItem.init(title: title ?? "", image: image?.withRenderingMode(.alwaysOriginal), selectedImage: selectedImage?.withRenderingMode(.alwaysOriginal))
        }
        
        
        do{
            let v = ViewController.init()
            createData(ctrs: &ctrols, vc: v, title: "1111", image: MZ.IMG("Suqare_unselected"), selectedImage: MZ.IMG("Suqare_selected"))
        }
        
        do{
            let v = TestViewController_1.init()
            createData(ctrs: &ctrols, vc: v, title: "2222", image: MZ.IMG("Suqare_unselected"), selectedImage: MZ.IMG("Suqare_selected"))
        }
        
        do{
            let v = TestViewController_2.init()
            createData(ctrs: &ctrols, vc: v, title: "3333", image: MZ.IMG("Suqare_unselected"), selectedImage: MZ.IMG("Suqare_selected"))
        }
        
        do{
            let v = TestViewController.init()
            createData(ctrs: &ctrols, vc: v, title: "4444", image: MZ.IMG("Suqare_unselected"), selectedImage: MZ.IMG("Suqare_selected"))
        }
        
        self.viewControllers = ctrols
        
        
    }
    
    func createTabbarChildViewController(_ vc: UIViewController, title: String?) -> UINavigationController {
        vc.title = title
        let navi = BaseNaviVCtrl.init(rootViewController: vc)
        navi.navigationBar.isHidden = true
        return navi
    }
    
    //UITabBarControllerDelegate
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        return true
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }
}
